package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.mian.data.MainDataTourTemplateDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataTravelTipsDTO;
import com.ctshk.rpc.system.entity.MainDataTourTemplate;
import com.ctshk.rpc.system.entity.MainDataTravelTips;
import com.ctshk.rpc.system.mapper.MainDataTourTemplateMapper;
import com.ctshk.rpc.system.req.main.data.MainDataTourTemplateIdReq;
import com.ctshk.rpc.system.req.main.data.MainDataTourTemplateQueryReq;
import com.ctshk.rpc.system.req.main.data.MainDataTourTemplateReq;
import com.ctshk.rpc.system.service.IMainDataTourTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系列团模板表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-28
 */
@DubboService
public class MainDataTourTemplateServiceImpl extends ServiceImpl<MainDataTourTemplateMapper, MainDataTourTemplate> implements IMainDataTourTemplateService {

    @Override
    public Result add(MainDataTourTemplateReq mainDataTourTemplateReq, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataTourTemplate mainDataTourTemplate = EntityUtil.copy(mainDataTourTemplateReq, MainDataTourTemplate.class);
        mainDataTourTemplate.setCreateId(tokenUser.getId());
        mainDataTourTemplate.setGmtCreate(now);
        mainDataTourTemplate.setModifiedId(tokenUser.getId());
        mainDataTourTemplate.setGmtModified(now);
        mainDataTourTemplate.setModifiedName(tokenUser.getUsername());
        mainDataTourTemplate.setIsDeleted(IsDeletedCode.NO.getCode());
        int insert = baseMapper.insert(mainDataTourTemplate);
        if(insert > 0){
            return Result.success();
        }
        return Result.failed(SystemError.MAINDATA_4028);
    }

    @Override
    public PageResponse<MainDataTourTemplateDTO> queryList(MainDataTourTemplateQueryReq mainDataTourTemplateQueryReq, TokenUser tokenUser) {
        IPage<MainDataTourTemplate> page = new Page<MainDataTourTemplate>(mainDataTourTemplateQueryReq.getPageNo(), mainDataTourTemplateQueryReq.getPageSize());
        QueryWrapper<MainDataTourTemplate> queryWrapper = new QueryWrapper<MainDataTourTemplate>();
        queryWrapper.lambda().eq(MainDataTourTemplate::getIsDeleted, 0).orderByDesc(MainDataTourTemplate::getGmtModified);

        if (null != mainDataTourTemplateQueryReq.getType()) {
            queryWrapper.lambda().eq(MainDataTourTemplate::getType, mainDataTourTemplateQueryReq.getType());
        }
        if (null != mainDataTourTemplateQueryReq.getStatus()) {
            queryWrapper.lambda().eq(MainDataTourTemplate::getStatus, mainDataTourTemplateQueryReq.getStatus());
        }
        if (StringUtils.isNotBlank(mainDataTourTemplateQueryReq.getName())) {
            queryWrapper.lambda().like(MainDataTourTemplate::getName, mainDataTourTemplateQueryReq.getName());
        }
        IPage<MainDataTourTemplate> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataTourTemplate> mainDataTourTemplateList = iPage.getRecords();
        if(CollectionUtils.isEmpty(mainDataTourTemplateList)){
            new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());

        }
        List<MainDataTourTemplateDTO> mainDataTourTemplateDTOS = EntityUtil.copyList(mainDataTourTemplateList, MainDataTourTemplateDTO.class);
        return new PageResponse<>(mainDataTourTemplateDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    public void delete(MainDataTourTemplateIdReq mainDataTourTemplateIdReq, TokenUser tokenUser) {
        MainDataTourTemplate mainDataTourTemplate = new MainDataTourTemplate();
        mainDataTourTemplate.setModifiedId(tokenUser.getId());
        mainDataTourTemplate.setGmtModified(LocalDateTime.now());
        mainDataTourTemplate.setModifiedName(tokenUser.getNickname());
        mainDataTourTemplate.setId(mainDataTourTemplateIdReq.getId());
        mainDataTourTemplate.setIsDeleted(1);
        baseMapper.updateById(mainDataTourTemplate);
    }

    @Override
    public void update(MainDataTourTemplateReq mainDataTourTemplateReq, TokenUser tokenUser) {
        MainDataTourTemplate mainDataTourTemplate = EntityUtil.copy(mainDataTourTemplateReq, MainDataTourTemplate.class);
        mainDataTourTemplate.setModifiedId(tokenUser.getId());
        mainDataTourTemplate.setGmtModified(LocalDateTime.now());
        mainDataTourTemplate.setModifiedName(tokenUser.getUsername());
        baseMapper.updateById(mainDataTourTemplate);
    }

    @Override
    public void updateStatus(MainDataTourTemplateIdReq mainDataTourTemplateIdReq, TokenUser tokenUser) {
        MainDataTourTemplate mainDataTourTemplate = baseMapper.selectById(mainDataTourTemplateIdReq.getId());
        if (null == mainDataTourTemplate) {
            throw new BusinessException(SystemError.SYS_411);
        }
        MainDataTourTemplate tourTemplate = new MainDataTourTemplate();
        if (mainDataTourTemplate.getStatus() == 0) {

            tourTemplate.setId(mainDataTourTemplateIdReq.getId());
            tourTemplate.setStatus(1);
        }
        if (mainDataTourTemplate.getStatus() == 1) {
            tourTemplate.setId(mainDataTourTemplateIdReq.getId());
            tourTemplate.setStatus(0);
        }
        baseMapper.updateById(tourTemplate);
    }

    @Override
    public MainDataTourTemplateDTO queryTemplateId(MainDataTourTemplateIdReq mainDataTourTemplateIdReq, TokenUser tokenUser) {

        MainDataTourTemplate mainDataTourTemplate = baseMapper.selectById(mainDataTourTemplateIdReq.getId());
        MainDataTourTemplateDTO mainDataTourTemplateDTO = new MainDataTourTemplateDTO();
        BeanUtils.copyProperties(mainDataTourTemplate, MainDataTourTemplateDTO.class);
        return mainDataTourTemplateDTO;
    }
}
